home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Library / Manuels & Misc / Assembly / AOA.ZIP / CH16 / EX16_1A.ASM < prev    next >
Encoding:
Assembly Source File  |  1996-03-27  |  5.3 KB  |  230 lines

  1. ; EX16_1a.asm
  2. ;
  3. ; A simple floating point calculator that demonstrates the use of the
  4. ; UCR Standard Library pattern matching routines.  Note that this
  5. ; program requires an FPU.
  6.  
  7.         .xlist
  8.         .386
  9.         .387
  10.         option        segment:use16
  11.         include     stdlib.a
  12.         includelib    stdlib.lib
  13.         matchfuncs
  14.         .list
  15.  
  16.  
  17. dseg        segment    para public 'data'
  18.  
  19. ; The following is a temporary used when converting a floating point
  20. ; string to a 64 bit real value.
  21.  
  22. CurValue    real8    0.0
  23.  
  24.  
  25. ; A Test String:
  26.  
  27. TestStr        byte    "5+2-(3-1)",0
  28.  
  29.  
  30. ; If the symbol "DEBUG" is defined, then call the MatchSP routine
  31. ; to do stack overflow checking.  If "DEBUG" is not defined, just
  32. ; call the sl_Match2 routine directly.
  33.  
  34. DEBUG        =    0        ;Define for debugging.
  35.  
  36.         ifdef    DEBUG
  37. MatchPat    textequ    <MatchSP>
  38.         else
  39. MatchPat    textequ    <sl_Match2>
  40.         endif
  41.  
  42.  
  43. ; Grammar for simple infix -> postfix translation operation:
  44. ; Semantic rules appear in braces.
  45. ;
  46. ; NOTE: This code has a serious problem.  The first production
  47. ; is left recursive and will generate an infinite loop.
  48. ;
  49. ; E -> E+T {print result} | T {print result}
  50. ; T -> <constant> {fld constant} | (E)
  51. ;
  52. ;
  53. ;
  54. ; UCR Standard Library Pattern that handles the grammar above:
  55.  
  56. ; An expression consists of an "E" item followed by the end of the string:
  57.  
  58. Expression      pattern {MatchPat,E,,EndOfString}
  59. EndOfString    pattern    {EOS}
  60.  
  61.  
  62. ; An "E" item consists of an "E" item optionally followed by "+" or "-"
  63. ; and a "T" item (E -> E+T | T):
  64.  
  65. E               pattern {MatchPat, E,T,Eplus}
  66. Eplus        pattern    {MatchChar, '+', T, epPlus}
  67. epPlus        pattern    {DoFadd}
  68.  
  69.  
  70. ; A "T" item is either a floating point constant or "(" followed by
  71. ; an "E" item followed by ")".
  72. ;
  73. ; The regular expression for a floating point constant is
  74. ;
  75. ;    [0-9]+ ( "." [0-9]* | ) ( ((e|E) (+|-| ) [0-9]+) | )
  76. ;
  77. ; Note: the pattern "Const" matches exactly the characters specified
  78. ;    by the above regular expression.  It is the pattern the calc-
  79. ;    ulator grabs when converting a string to a floating point number.
  80.  
  81.  
  82. Const           pattern {MatchPat, ConstStr, 0, FLDConst}
  83. ConstStr        pattern {MatchPat, DoDigits, 0, Const2}
  84. Const2        pattern    {matchchar, '.', Const4, Const3}
  85. Const3          pattern {MatchPat, DoDigits, Const4, Const4}
  86. Const4        pattern    {matchchar, 'e', const5, const6}
  87. Const5        pattern    {matchchar, 'E', Succeed, const6}
  88. Const6        pattern    {matchchar, '+', const7, const8}
  89. Const7        pattern    {matchchar, '-', const8, const8}
  90. Const8          pattern {MatchPat, DoDigits}
  91.  
  92. FldConst    pattern    {PushValue}
  93.  
  94. ; DoDigits handles the regular expression [0-9]+
  95.  
  96. DoDigits    pattern    {Anycset, Digits, 0, SpanDigits}
  97. SpanDigits    pattern    {Spancset, Digits}
  98.  
  99. ; The S production handles constants or an expression in parentheses.
  100.  
  101. T        pattern    {MatchChar, '(', Const, IntE}
  102. IntE            pattern {MatchPat, E, 0, CloseParen}
  103. CloseParen    pattern    {MatchChar, ')'}
  104.  
  105.  
  106. ; The Succeed pattern always succeeds.
  107.  
  108. Succeed        pattern    {DoSucceed}
  109.  
  110.  
  111. ; We use digits from the UCR Standard Library cset standard sets.
  112.  
  113.         include    stdsets.a
  114.  
  115. dseg        ends
  116.  
  117.  
  118.  
  119. cseg        segment    para public 'code'
  120.         assume    cs:cseg, ds:dseg
  121.  
  122. ; Debugging feature #1:
  123. ; This is a special version of sl_Match2 that checks for
  124. ; stack overflow.  Stack overflow occurs whenever there
  125. ; is an infinite loop (i.e., left recursion) in a pattern.
  126.  
  127. MatchSP        proc    far
  128.         cmp    sp, offset StkOvrfl
  129.         jbe    AbortPgm
  130.         jmp    sl_Match2
  131.  
  132. AbortPgm:    print
  133.                 byte    cr,lf,lf
  134.         byte    "Error: Stack overflow in MatchSP routine.",cr,lf,0
  135.         ExitPgm
  136. MatchSP          endp
  137.  
  138.  
  139. ; DoSucceed matches the empty string.  In other words, it matches anything
  140. ; and always returns success without eating any characters from the input
  141. ; string.
  142.  
  143. DoSucceed    proc    far
  144.         mov    ax, di
  145.         stc
  146.         ret
  147. DoSucceed    endp
  148.  
  149.  
  150. ; DoFadd - Adds the two items on the top of the FPU stack.
  151.  
  152. DoFadd        proc    far
  153.         faddp    st(1), st
  154.         mov    ax, di            ;Required by sl_Match
  155.         stc                ;Always succeed.
  156.         ret
  157. DoFadd        endp
  158.  
  159.  
  160. ; PushValue-    We've just matched a string that corresponds to a
  161. ;        floating point constant.  Convert it to a floating
  162. ;        point value and push that value onto the FPU stack.
  163.  
  164. PushValue    proc    far
  165.         push    ds
  166.         push    es
  167.         pusha
  168.         mov    ax, dseg
  169.         mov    ds, ax
  170.  
  171.         lesi    Const        ;FP val matched by this pat.
  172.         patgrab            ;Get a copy of the string.
  173.         atof            ;Convert to real.
  174.         free            ;Return mem used by patgrab.
  175.         lesi    CurValue    ;Copy floating point accumulator
  176.         sdfpa            ; to a local variable and then
  177.         fld    CurValue    ; copy that value to the FPU stk.
  178.  
  179.         popa
  180.         mov    ax, di
  181.         pop    es
  182.         pop    ds
  183.         stc
  184.         ret
  185. PushValue    endp
  186.  
  187.  
  188.  
  189. ; The main program tests the expression evaluator.
  190.  
  191. Main        proc
  192.         mov    ax, dseg
  193.         mov    ds, ax
  194.         mov    es, ax
  195.         meminit
  196.  
  197.         finit            ;Be sure to do this!
  198.         fwait
  199.  
  200.         lesi    TestStr
  201.         puts            ;Print the expression
  202.  
  203.         ldxi    Expression
  204.         xor    cx, cx
  205.         match
  206.         jc    GoodVal
  207.         printff
  208.         byte    " is an illegal expression",cr,lf,0
  209.         ret
  210.  
  211. GoodVal:    fstp    CurValue
  212.         printff
  213.         byte    " = %12.6ge\n",0
  214.         dword    CurValue
  215.  
  216. Quit:        ExitPgm
  217. Main        endp
  218. cseg        ends
  219.  
  220. sseg        segment    para stack 'stack'
  221.         word    64 dup (?)        ;Buffer for stack overflow
  222. StkOvrfl    word    ?            ;Stack overflow if drops
  223. stk        db    1024 dup ("stack   ")    ; below StkOvrfl.
  224. sseg        ends
  225.  
  226. zzzzzzseg    segment    para public 'zzzzzz'
  227. LastBytes    db    16 dup (?)
  228. zzzzzzseg    ends
  229.         end    Main
  230.